#!/usr/bin/env Python
# -*- coding:utf-8 -*-
# multifunctional_unit_converter.py
# author: luoxiang


# 需求：实现以下常见公制单位的相互转换

# 1.完成温度互转，华氏温度与摄氏温度
# 温度单位:摄氏度(C)，华氏度(F)，开氏度(K)

# 2.完成长度互转，中国与美国长度单位
# 长度单位：公里(km)，米(m)，厘米(cm)，英里(mile)，码(yard)，英尺(ft)，英寸(inch)

# 3.完成货币互转，美元与人民币（汇率以你作业当天为准）
# 货币单位：人民币(CNY)，美元(USD)

import time
import math
type_dict = {}
unit_dict = {}

num = 0
temp = {1: '摄氏度(C)', 2: '华氏度(F)', 3: '开氏度(K)'}
length = {1: '公里(km)', 2: '米(m)', 3: '厘米(cm)', 4: '英里(mile)',
          5: '码(yard)', 6: '英尺(ft)', 7: '英寸(inch)'}
currency = {1: '人民币(CNY)', 2: '美元(USD)'}
unit_dict['temp'] = temp
unit_dict['length'] = length
unit_dict['currency'] = currency
type_name = '''temp,length,currency'''
type_ch_name = '温度,长度,货币'
type_list = type_name.split(',')
type_ch_list = type_ch_name.split(',')
for x in type_list:
    num += 1
    type_dict[num] = x

print('欢迎使用多功能单位转换器'.center(30, "-"))
time.sleep(1)
print('转换器提供3种转换类型'.center(30, "-"))
time.sleep(1)
print('1.温度 2.长度 3.货币 0.退出 ')
time.sleep(1)
choose = input('请输入对应数字：')

while choose == '':
    print('不能为空！')
    choose = input('请输入对应数字：')
    continue     

choose = int(choose)

while choose == '0':  # 退出
    break

# 温度单位互转
if choose == 1:
    
    print(f'您选择的{type_ch_list[choose -1]}有以下单位可供互转')
    time.sleep(1)
    print(unit_dict[type_dict[choose]])

while choose == 1:

    value01 = input(f'请输入{type_ch_list[choose -1]}(示例:1C, 2F, 3K):')

    if value01 in ['C', 'F', 'K']:
        print('单位前不能为空')
        continue
    
    # 摄氏温度转其他
    if value01[value01.find('C'):] == 'C':
        TC = value01[:value01.find('C')]
        TF = (9/5) * float(TC) + 32
        TK = float(TC) + 273.15
        print(f'{unit_dict[type_dict[choose]][2]}: {float(TF)},',
              f'{unit_dict[type_dict[choose]][3]}: {float(TK)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break
    
    # 华氏温度转其他
    if value01[value01.find('F'):] == 'F':
        TF = value01[:value01.find('F')]
        TC = (5/9) * (float(TF) - 32)
        TK = (5/9) * (float(TF) - 32) + 273.15
        print(f'{unit_dict[type_dict[choose]][1]}: {float(TC)},',
              f'{unit_dict[type_dict[choose]][3]}: {float(TK)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break

    # 开氏温度转其他
    if value01[value01.find('K'):] == 'K':
        TK = value01[:value01.find('K')]
        TC = float(TK) - 273.15
        TF = (9/5) * (float(TK) - 273.15) + 32
        print(f'{unit_dict[type_dict[choose]][1]}: {float(TC)},',
              f'{unit_dict[type_dict[choose]][2]}: {float(TF)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break
    print('格式不正确！')

else:
    print('谢谢使用！')

# 长度单位互转
if choose == 2:

    print(f'您选择的{type_ch_list[choose -1]}有以下单位可供互转')
    time.sleep(1)
    print(unit_dict[type_dict[choose]])

while choose == 2:

    value02 = input(f'请输入{type_ch_list[choose -1]}(示例:1km, 2m, 3cm):')

    if value02 in ['km', 'm', 'cm', 'mile', 'yard', 'ft', 'inch']:
        print('单位前不能为空!')
        continue

    # 公里转其他
    if value02[value02.find('km'):] == 'km':
        km = value02[:value02.find('km')]
        m = float(km) * 10**3
        cm = float(km) * 10**5
        mile = float(km) * 0.6213712
        yard = float(km) * 1093.6132983
        ft = float(km) * 3280.839895
        inch = float(km) * 39370.0787402
        print(f'{unit_dict[type_dict[choose]][2]}: {float(m)},',
              f'{unit_dict[type_dict[choose]][3]}: {float(cm)},',
              f'{unit_dict[type_dict[choose]][4]}: {float(mile)},',
              f'{unit_dict[type_dict[choose]][5]}: {float(yard)},',
              f'{unit_dict[type_dict[choose]][6]}: {float(ft)},',
              f'{unit_dict[type_dict[choose]][7]}: {float(inch)}')
        time.sleep(1)
        print('是否继续转换(y/n)')  #判断是否继续转换
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break
    
    # 厘米转其他
    if value02[value02.find('cm'):] == 'cm':
        cm = value02[:value02.find('cm')]
        km = float(cm) * pow(10, -5)
        m = float(cm) * 10**(-2)
        mile = float(cm) * 6.2137e-6
        yard = float(cm) * 0.0109361
        ft = float(cm) * 0.0328084
        inch = float(cm) * 0.3937008
        print(f'{unit_dict[type_dict[choose]][1]}: {float(km)},',
              f'{unit_dict[type_dict[choose]][2]}: {float(m)},',
              f'{unit_dict[type_dict[choose]][4]}: {float(mile)},',
              f'{unit_dict[type_dict[choose]][5]}: {float(yard)},',
              f'{unit_dict[type_dict[choose]][6]}: {float(ft)},',
              f'{unit_dict[type_dict[choose]][7]}: {float(inch)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break

    # 米转其他
    if value02[value02.find('m'):] == 'm':
        m = value02[:value02.find('m')]
        km = float(m) * 10**(-3)
        cm = float(m) * 100
        mile = float(m) * 0.0006214
        yard = float(m) * 1.0936133
        ft = float(m) * 3.2808399
        inch = float(m) * 39.3700787
        print(f'{unit_dict[type_dict[choose]][1]}: {float(km)},',
              f'{unit_dict[type_dict[choose]][3]}: {float(cm)},',
              f'{unit_dict[type_dict[choose]][4]}: {float(mile)},',
              f'{unit_dict[type_dict[choose]][5]}: {float(yard)},',
              f'{unit_dict[type_dict[choose]][6]}: {float(ft)},',
              f'{unit_dict[type_dict[choose]][7]}: {float(inch)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break
 
    # 英里转其他
    if value02[value02.find('mile'):] == 'mile':
        mile = value02[:value02.find('mile')]
        km = float(mile) * 1.609344
        m = float(mile) * 1609.344
        cm = float(mile) * 160934.4
        yard = float(mile) * 1760
        ft = float(mile) * 5280
        inch = float(mile) * 63360
        print(f'{unit_dict[type_dict[choose]][1]}: {float(km)},',
              f'{unit_dict[type_dict[choose]][2]}: {float(m)},',
              f'{unit_dict[type_dict[choose]][3]}: {float(cm)},',
              f'{unit_dict[type_dict[choose]][5]}: {float(yard)},',
              f'{unit_dict[type_dict[choose]][6]}: {float(ft)},',
              f'{unit_dict[type_dict[choose]][7]}: {float(inch)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break
 
    # 码转其他
    if value02[value02.find('yard'):] == 'yard':
        yard = value02[:value02.find('yard')]
        km = float(yard) * 0.0009144
        m = float(yard) * 0.9144
        cm = float(yard) * 91.44
        mile = float(yard) * 0.0005682
        ft = float(yard) * 3
        inch = float(yard) * 36
        print(f'{unit_dict[type_dict[choose]][1]}: {float(km)},',
              f'{unit_dict[type_dict[choose]][2]}: {float(m)},',
              f'{unit_dict[type_dict[choose]][3]}: {float(cm)},',
              f'{unit_dict[type_dict[choose]][4]}: {float(mile)},',
              f'{unit_dict[type_dict[choose]][6]}: {float(ft)},',
              f'{unit_dict[type_dict[choose]][7]}: {float(inch)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break

    # 英尺转其他
    if value02[value02.find('ft'):] == 'ft':
        ft = value02[:value02.find('ft')]
        km = float(ft) * 0.0003048
        m = float(ft) * 0.3048
        cm = float(ft) * 30.48
        mile = float(ft) * 0.0001894
        yard = float(ft) * 0.3333333
        inch = float(ft) * 12
        print(f'{unit_dict[type_dict[choose]][1]}: {float(km)},',
              f'{unit_dict[type_dict[choose]][2]}: {float(m)},',
              f'{unit_dict[type_dict[choose]][3]}: {float(cm)},',
              f'{unit_dict[type_dict[choose]][4]}: {float(mile)},',
              f'{unit_dict[type_dict[choose]][5]}: {float(yard)},',
              f'{unit_dict[type_dict[choose]][7]}: {float(inch)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break

    # 英寸转其他
    if value02[value02.find('inch'):] == 'inch':
        inch = value02[:value02.find('inch')]
        km = float(inch) * 0.0000254
        m = float(inch) * 0.0254
        cm = float(inch) * 2.54
        mile = float(inch) * 0.0000158
        yard = float(inch) * 0.0277778
        ft = float(inch) * 0.0833333
        print(f'{unit_dict[type_dict[choose]][1]}: {float(km)},',
              f'{unit_dict[type_dict[choose]][2]}: {float(m)},',
              f'{unit_dict[type_dict[choose]][3]}: {float(cm)},',
              f'{unit_dict[type_dict[choose]][4]}: {float(mile)},',
              f'{unit_dict[type_dict[choose]][5]}: {float(yard)},',
              f'{unit_dict[type_dict[choose]][6]}: {float(ft)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break   
    print('格式不正确！')

else:
    print('谢谢使用！')

# 货币单位互转
if choose == 3:

    print(f'您选择的{type_ch_list[choose -1]}有以下单位可供互转')
    time.sleep(1)
    print(unit_dict[type_dict[choose]])
    time.sleep(1)
    print('汇率仅供参考，交易时以银行柜台成交价为准 更新时间:2019-07-18 23:07')
while choose == 3:

    value03 = input(f'请输入{type_ch_list[choose -1]}(示例:100CNY, 10USD):')

    if value03 in ['CNY', 'USD']:
        print('单位前不能为空!')
        continue

    # 2019年7月18日23:00汇率: 1人民币=0.1454美元, 1美元=6.8792人民币
    #人民币转美元
    if value03[value03.find('CNY'):] == 'CNY':
        CNY = value03[:value03.find('CNY')]
        USD = float(CNY) * 0.1454
        print(f'{unit_dict[type_dict[choose]][2]}: {float(USD)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break
    
    #美元转人名币
    if value03[value03.find('USD'):] == 'USD':
        USD = value03[:value03.find('USD')]
        CNY = float(USD) * 6.8792
        print(f'{unit_dict[type_dict[choose]][1]}: {float(CNY)}')
        time.sleep(1)
        print('是否继续转换(y/n)')
        is_continue = input('')
        if is_continue == 'y':
            continue
        else:
            break
    print('格式不正确！')

else:
    print('谢谢使用！')